//
//  LBMainViewController.swift
//  LBNewWeiBo
//
//  Created by bin on 15/10/26.
//  Copyright © 2015年 bin. All rights reserved.
//

import UIKit

class LBMainViewController: UITabBarController {

    
    
    /// 按钮的点击事件
     func composeButtonClick() {
        print(__FUNCTION__) // 打印方法的名称
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.tabBar.tintColor = UIColor.orangeColor()
        // 首页
        let homeVc = LBHomeTableViewController()
        addChildViewControllers(homeVc, title: "首页", imageName: "tabbar_home")
        
        // 消息
        let messageVc = LBMessageTableViewController()
        addChildViewControllers(messageVc, title: "消息", imageName: "tabbar_message_center")

        let buttonVc = UIViewController()
        self.addChildViewControllers(buttonVc, title: "", imageName: "x")
        
        // 发现
        let discover = LBDiscoverTableViewController()
        addChildViewControllers(discover, title: "发现", imageName: "tabbar_discover")

        // 我
        let profileVc = LBPorfileTableViewController()
        addChildViewControllers(profileVc, title: "我", imageName: "tabbar_profile")
    }

    /**
    当控制器将要加载视图的时候调用该方法
    
    - parameter animated: 动画
    */
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        let width = self.tabBar.bounds.width / CGFloat(5) //每个item的宽度
        // 设置按钮的frame
        self.composeButton.frame = CGRect(x: width*2, y: 0, width: width, height: self.tabBar.bounds.height)
        self.tabBar .addSubview(self.composeButton)
    }
    
    
    /**
    添加自控制器，包装Nav
    
    - parameter controller: 自控制器
    - parameter title:      标题
    - parameter imageName:  图片的名字
    */
  private func addChildViewControllers(controller: UIViewController, title: String, imageName: String){
        
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        addChildViewController(UINavigationController(rootViewController: controller))
        
    }
    
    // MARK: - 懒加载
    /// 撰写按钮的懒加载
    lazy var composeButton: UIButton = {
        let composeBtn = UIButton()
        
    // 添加图片
        composeBtn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal) // 普通状态
        composeBtn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 添加按钮的背景
        composeBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        composeBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 按钮的点击事件
        composeBtn.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)

        return composeBtn
    }()

}
